/**
 * @fileoverview 位置状态栏组件，用于显示鼠标位置和相机信息
 * @author your name
 * @version 1.0.0
 */

import * as Cesium from 'cesium'
import PositionTransfer from './positionHandler'

/**
 * 位置状态栏类
 * @class PositionStatusBar
 * @description 创建一个位置状态栏，显示鼠标经纬度、高度以及相机姿态信息
 */
class PositionStatusBar {
    /**
     * 创建位置状态栏实例
     * @param {Cesium.Viewer} viewer - Cesium viewer实例
     */
    constructor(viewer){
        /**
         * @private
         * @type {Cesium.Viewer}
         */
        this.viewer = viewer
        /**
         * @private
         * @type {PositionTransfer}
         */
        this.positionTransfer = new PositionTransfer(viewer)
        this.createDom()
        this.initEvent()
    }

    /**
     * 创建状态栏DOM元素
     * @private
     * @description 创建显示位置信息的DOM元素，包括经纬度、高度、相机姿态等信息
     */
    createDom(){
        const barContainer=document.createElement('div')
        barContainer.className='position-bar'
        // 吧这个提示工具放在cesium容器下面
		this.viewer.cesiumWidget.container.appendChild(barContainer);
        // 鼠标的经纬度，高度
        this.divLng=document.createElement('div')
        barContainer.appendChild(this.divLng)

        this.divLat=document.createElement('div')
        barContainer.appendChild(this.divLat)

        this.divHeight=document.createElement('div')
        barContainer.appendChild(this.divHeight)

        // 相机信息，三个角度以及相机高度
        this.divHeading=document.createElement('div')
        barContainer.appendChild(this.divHeading)

        this.divPitch=document.createElement('div')
        barContainer.appendChild(this.divPitch)

        this.divCameraHeight=document.createElement('div')
        barContainer.appendChild(this.divCameraHeight)
    }

    /**
     * 初始化事件监听
     * @private
     * @description 初始化鼠标移动事件监听和相机姿态变化监听
     */
    initEvent(){
        /**
         * @private
         * @type {Cesium.ScreenSpaceEventHandler}
         */
        this.mouseHandler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas)
        this.mouseHandler.setInputAction(e => {
            const {endPosition} = e
            const cartesian3 = this.positionTransfer.ScreenToCartesian(endPosition)
            if(cartesian3){
                const {lng, lat, height} = this.positionTransfer.CartesianToDegree(cartesian3)
                this.divLng.textContent = lng.toFixed(4)
                this.divLat.textContent = lat.toFixed(4)
                this.divHeight.textContent = height.toFixed(4)
            }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)

        // 监听相机的变化事件，得到相机信息
        this.viewer.scene.camera.changed.addEventListener(this.cameraEvent,this)
    }

    cameraEvent(){
        const {heading,pitch,position}=this.viewer.scene.camera
        // 将heading和pitch转为角度
        const headingDegree=Cesium.Math.toDegrees(heading)
        const pitchDegree=Cesium.Math.toDegrees(pitch)
        // 再将position笛卡尔坐标转到经纬度
        const {height}=this.positionTransfer.CartesianToDegree(position)
        this.divCameraHeight.textContent=height.toFixed(4)
        this.divHeading.textContent=headingDegree.toFixed(2)
        this.divPitch.textContent=pitchDegree.toFixed(2)
    }

    /**
     * 显示位置状态栏
     * @public
     * @description 显示位置状态栏并重新启用事件监听
     */
    show(){
        const container = document.querySelector('.position-bar')
        if (container) {
            container.style.display = 'flex'
        }
        // 重新启用事件监听
        if (this.mouseHandler) {
            this.mouseHandler.setInputAction(e => {
                const {endPosition} = e
                const cartesian3 = this.positionTransfer.ScreenToCartesian(endPosition)
                if(cartesian3){
                    const {lng, lat, height} = this.positionTransfer.CartesianToDegree(cartesian3)
                    this.divLng.textContent = lng.toFixed(4)
                    this.divLat.textContent = lat.toFixed(4)
                    this.divHeight.textContent = height.toFixed(4)
                }
            }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
        }
        // 重新添加相机事件监听
        this.viewer.scene.camera.changed.addEventListener(this.cameraEvent, this)
    }

    /**
     * 隐藏位置状态栏
     * @public
     * @description 隐藏位置状态栏并暂停事件监听以提高性能
     */
    hide(){
        const container = document.querySelector('.position-bar')
        if (container) {
            container.style.display = 'none'
        }
        // 暂停事件监听以提高性能
        if (this.mouseHandler) {
            this.mouseHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
        }
        // 移除相机事件监听
        this.viewer.scene.camera.changed.removeEventListener(this.cameraEvent, this)
    }

    /**
     * 销毁位置状态栏
     * @public
     * @description 清理事件监听器和DOM元素
     */
    destroy() {
        if (this.mouseHandler) {
            this.mouseHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            this.mouseHandler = null
        }

        this.viewer.scene.camera.changed.removeEventListener(this.cameraEvent)
        
        // 移除DOM元素
        const container = document.querySelector('.position-bar')
        if (container) {
            container.remove()
        }
    }
}

export default PositionStatusBar